//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kun.portal.webframe.controller;

import com.kun.framework.core.util.DateUtils;
import com.kun.framework.web.web.vo.ValueLabelVo;
import com.kun.portal.webframe.dao.FwModuleDao;
import com.kun.portal.webframe.exception.FwBaseException;
import com.kun.portal.webframe.po.FwModulePo;
import com.kun.portal.webframe.po.FwTeamPo;
import com.kun.portal.webframe.po.FwUserPo;
import com.kun.portal.webframe.service.FwModuleService;
import com.kun.portal.webframe.service.FwTeamService;
import com.kun.portal.webframe.util.HeaderUtil;
import com.kun.portal.webframe.util.UserSessionUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping({"/api/webframe"})
public class FwModuleController {
    private static final Logger LOG = LoggerFactory.getLogger(FwModuleController.class);
    @Autowired
    private FwModuleService fwmoduleService;
    @Autowired
    private FwModuleDao fwModuleDao;
    @Autowired
    private FwTeamService teamService;

    public FwModuleController() {
    }

    @RequestMapping(
            value = {"/fwmodule"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Page<FwModulePo> findByCreatorAndModuleNameContaining(@RequestParam String type, @RequestParam(required = false) String key, Pageable pageable, HttpSession session) {
        key = key == null?"":key;
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        return this.fwmoduleService.findByCreatorAndModuleTypeAndModuleNameContaining(user.getUserId(), type, key, pageable);
    }

    @RequestMapping(
            value = {"/fwmodulePermissionTree"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<FwModulePo> fwmodulePermissionTree(@RequestBody Map map, HttpSession session) {
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        FwTeamPo team = UserSessionUtil.getCurrentUserTeam(session);
        String parentCode = (String)map.get("ID");
        return parentCode != null && !"".equals(parentCode)?this.fwmoduleService.fwmodulePermissionTree(parentCode, team.getTeamId(), user.getUserId()):this.fwmoduleService.fwmodulePermissionTree(user.getUserId());
    }


    @RequestMapping(
            value = {"/fwmodulePermissionTree"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<FwModulePo> fwmodulePermissionTreeG( HttpSession session) {
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        return this.fwmoduleService.fwmodulePermissionTree(user);
    }

    @RequestMapping(
            value = {"/fwmoduleTree"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<FwModulePo> findByModuleNameLikeTree(@RequestParam(required = false) String type, @RequestParam(required = false) String key, HttpSession session) {
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        return key == null?this.fwmoduleService.findByCreatorAndModuleTypeAndParentCodeIsNullOrderByModuleId(user.getUserId(), type):this.fwmoduleService.findByCreatorAndModuleTypeAndParentCodeEqualsOrderByModuleId(user.getUserId(), type, key);
    }

    @RequestMapping(
            value = {"/fwmoduleMainPage"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<FwModulePo> fwmoduleMainPage(HttpSession session) {
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        FwTeamPo team = UserSessionUtil.getCurrentUserTeam(session);
        return this.fwmoduleService.findByParentCodeEqualsOrderByModuleSeq("02", team.getTeamId(), user.getUserId());
    }

    @RequestMapping(
            value = {"/fwmodule/transferByRole/{roleId}"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Map queryModuleByRoleId(@PathVariable("roleId") String roleId, HttpSession session) {
        Map result = new HashMap();
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        List<ValueLabelVo> allcreated = this.fwmoduleService.findFwModuleByCreator(user.getUserId());
        List<String> selected = this.fwmoduleService.findFwModuleByRoleId(roleId);
        result.put("dataModules", allcreated);
        result.put("myModules", selected);
        return result;
    }

    @RequestMapping(
            value = {"/fwmodule/tree/role/{roleId}"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Map queryModuleTreeByRoleId(@PathVariable("roleId") String roleId, HttpSession session) {
        Map result = new HashMap();
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        //FwTeamPo team = UserSessionUtil.getCurrentUserTeam(session);
        String userid = user.getUserId();
        //String teamid = team.getTeamId();
        List<Map<String, Object>> allData = this.fwmoduleService.findParentModuleByUser(null, userid);
        List<String> selected = this.fwmoduleService.findFwModuleByRoleId(roleId);
        result.put("allData", allData);
        result.put("selectId", selected);
        return result;
    }

    @RequestMapping(
            value = {"/fwmodule/tree/team"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Map queryModuleTreeByTeamId(@RequestParam(required = false,defaultValue = "") String parentId, @RequestParam(required = false,defaultValue = "") String teamId, HttpSession session) {
        Map result = new HashMap();
        if(!StringUtils.isEmpty(teamId) && (!StringUtils.isEmpty(parentId) || !StringUtils.isEmpty(teamId))) {
            if(StringUtils.isEmpty(parentId) && !"-1".equals(teamId)) {
                parentId = this.teamService.findByTeamId(teamId).getParentCode();
            }

            List<Map<String, Object>> allData = this.fwmoduleService.findParentModuleByTeamId(parentId);
            List<String> selected = this.fwmoduleService.findFwModuleByTeamId(teamId);
            result.put("allData", allData);
            result.put("selectId", selected);
            return result;
        } else {
            throw new FwBaseException("请先选择团队");
        }
    }

    @RequestMapping(
            value = {"/fwmodule/updateFwRoleModuleByRoleId"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public Map updateFwRoleModuleByRoleId(@RequestBody Map map, HttpSession session) {
        List<String> moduleIds = (List)map.get("moduleIds");
        String roleId = (String)map.get("roleId");
        this.fwmoduleService.updateFwRoleModuleByRoleId(moduleIds, roleId);
        return null;
    }

    @RequestMapping(
            value = {"/fwmodule/updateFwRoleModuleByModuleId"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public Map updateFwRoleModuleByModuleId(@RequestBody Map map, HttpSession session) {
        List<String> roleIds = (List)map.get("roleIds");
        String moduleId = (String)map.get("moduleId");
        this.fwmoduleService.updateFwRoleModuleByModuleId(roleIds, moduleId);
        return null;
    }

    @RequestMapping(
            value = {"/fwmodule"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public ResponseEntity<FwModulePo> addFwModule(@RequestBody FwModulePo fwmodule) {
        FwModulePo metaFwModule = this.fwmoduleService.addFwModule(fwmodule);
        return new ResponseEntity(metaFwModule, HttpStatus.OK);
    }

    @RequestMapping(
            value = {"/fwmodule"},
            method = {RequestMethod.PUT}
    )
    @ResponseBody
    public ResponseEntity<FwModulePo> updateFwModule(@RequestBody FwModulePo fwmodule, HttpSession session) {
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        String moduleCode = fwmodule.getModuleCode();
        if(moduleCode.equals("")) {
            String parentCode = fwmodule.getParentCode();
            int moduleCodeLen = parentCode == null?2:parentCode.length() + 2;
            String maxModuleCode = this.fwmoduleService.findMaxModuleCodeByParentCode(parentCode);
            moduleCode = maxModuleCode == null?"1":String.valueOf(Long.valueOf(maxModuleCode).longValue() + 1L);
            moduleCode = moduleCode.length() < moduleCodeLen?"0" + moduleCode:moduleCode;
            if(parentCode != null && moduleCode.length() <= parentCode.length()) {
                moduleCode = parentCode + moduleCode;
            }

            fwmodule.setModuleCode(moduleCode);
        }

        fwmodule.setCreator(user.getUserId());
        fwmodule.setCreateTime(DateUtils.getNowDate());
        fwmodule.setModifyTime(DateUtils.getNowDate());
        FwModulePo metaFwModule = this.fwmoduleService.updateFwModule(fwmodule);
        return new ResponseEntity(metaFwModule, HttpStatus.OK);
    }

    @RequestMapping(
            value = {"/fwmodule/{moduleId}"},
            method = {RequestMethod.DELETE}
    )
    @ResponseBody
    public ResponseEntity<Void> deleteFwModule(@PathVariable("moduleId") String moduleId) {
        this.fwmoduleService.deleteFwModule(moduleId);
        return ((BodyBuilder)ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("fwmodule", moduleId))).build();
    }

    @RequestMapping(
            value = {"/fwmodule/subModule"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public List<FwModulePo> getSubFwModule(@RequestParam(value = "parentCode",required = false) String parentCode) {
        return this.fwModuleDao.findByParentCodeOrderByModuleSeq(parentCode);
    }
}
